package com.cyf.common.core.io.fileLimit;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author chenyifan
 * @create 2024-04-28 17:55
 */
public class FileLimitInputStream extends InputStream {

    private InputStream inputStream;
    private FileSpeedLimiter fileSpeedLimiter;

    public FileLimitInputStream(InputStream inputStream, FileSpeedLimiter fileSpeedLimiter) {
        this.inputStream = inputStream;
        this.fileSpeedLimiter = fileSpeedLimiter;
    }

    @Override
    public int read() throws IOException {
        fileSpeedLimiter.limitNextBytes();
        return inputStream.read();
    }

    @Override
    public int read(byte[] b) throws IOException {
        fileSpeedLimiter.limitNextBytes(b.length);
        return inputStream.read(b);
    }

    @Override
    public int read(byte[] b, int off, int len) throws IOException {
        fileSpeedLimiter.limitNextBytes(len);
        return inputStream.read(b, off, len);
    }

}
